package TUILayer;

import java.util.ArrayList;
import java.util.Scanner;

import ControlLayer.DVDCtr;
import ModelLayer.DVD;
//import ModelLayer.Person;

import static TUILayer.TUIHelpers.keyboardInput;

public class DvdUI {
    private DVDCtr dvdCtr;//DVD UI must know the DVD controller 

    public DvdUI() {

    }

    public void start(DVDCtr ctr) {
        dvdCtr = ctr;
        dvdMenu();
    }

    public void dvdMenu() {
        boolean exit = false;
        while (!exit) //! means while exit not is true (that is: false)
        {
            int choice = writeDVDMenu();

            switch (choice) {
                case 1:
                    findDVD();
                    break;
                case 2:
                    createDVD();
                    break;
                case 3:
                    createCopy();
                    break;
                case 4:
                    //deleteDVD();
                    break;
                case 5:
                    listAllDVDs();
                    break;
                case 6:
                    exit = true;
                    break;
                default:
                    System.out.println("Not valid choice.");
                    exit = true;
                    break;
            }

        }//end while
    }

    private int writeDVDMenu() {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f *** DVD Menu ***");
        System.out.println(" (1) Show DVD information");
        System.out.println(" (2) Create DVD description");
        System.out.println(" (3) Create DVD copy");
        /*System.out.println(" (4) Delete DVD");*/
        System.out.println(" (5) Show current DVD list");
        System.out.println(" (6) Return");
        System.out.print("\n Make your choice: ");

        int choice = 0;
        try {
            choice = keyboard.nextInt();
        }
        catch (Exception e) {
            System.out.println("You must enter a number -  try again");
            String input = keyboard.nextLine();
        }
        return choice;
    }


    //there is need for more methods to read information about DVDs
    //and methods to display the information

    private void findDVD() {
        int no = Integer.parseInt(keyboardInput("Type DVD ID :"));
        //is to be changed when the DVD controller is completed
        DVD dvd = dvdCtr.findDVD(no);

        if (dvd != null) {
            System.out.println(dvd.getArtist());
            System.out.println(dvd.getTitle());
            System.out.println(dvd.getPublicationdate());
        }

    }

    private void createDVD() {
        String title = keyboardInput("Type DVD Title :");
        String artist = keyboardInput("Type DVD Artist :");

        //is to be changed when the DVD controller is completed
        dvdCtr.createDVD(title, artist);
    }

    private void createCopy() {
        int id = Integer.parseInt(keyboardInput("Type DVD ID : "));
        int price = Integer.parseInt(keyboardInput("Type DVD Price : "));

        dvdCtr.createCopy(id, price);
    }

/*    private void deleteDVD() {
        int id = Integer.parseInt(keyboardInput("Type DVD ID : "));

        dvdCtr.deleteDVD(id);
    }*/

    private void listAllDVDs() {
        ArrayList<DVD> dvds = dvdCtr.listAllDVDs();

        for (DVD dvd : dvds) {
            if (dvd != null) {
                System.out.println(dvd.getTitle());
                System.out.println(dvd.getArtist());
                System.out.println(dvd.getPublicationdate());

                System.out.println("------------");
            }
        }
    }
}